package com.his.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.his.dao.MedicalInstitDAO;
import com.his.dao.domain.MedicalInstit;
import com.his.struts.dao.curevideo.CVideoImp;
import com.his.struts.util.DbUtil;

/**
 * 
 * @author sahala
 *
 */
public class MedicalInstitDAOImpl implements MedicalInstitDAO {
	Logger log = Logger.getLogger(MedicalInstitDAOImpl.class);
	DbUtil db = new DbUtil();

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.his.dao.MedicalInstitDAO#addMedicalInstit(com.his.dao.domain.
	 * MedicalInstit)
	 */
	public void addMedicalInstit(MedicalInstit mi) {
		String sql = "insert into medical_instit (name,serverLimit,type,province,city,county,detailAddr,phone,detaildesc) values (?,?,?,?,?,?,?,?,?)";
		List<Object> params = new ArrayList<Object>();
		params.add(mi.getName());
		params.add(mi.getServerLimit());
		params.add(mi.getType());
		params.add(mi.getProvince());
		params.add(mi.getCity());
		params.add(mi.getCounty());
		params.add(mi.getDetailAddr());
		params.add(mi.getPhone());
		params.add(mi.getDetaildesc());

		try {
			db.executeUpdate(sql, params);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			db.closeAll();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.his.dao.MedicalInstitDAO#updateMedicalInstit(com.his.dao.domain.
	 * MedicalInstit)
	 */
	public void updateMedicalInstit(MedicalInstit mi) {
		String sql = "update medical_instit set name=?,serverLimit=?,type=?,province=?,city=?,county=?,detailAddr=?,phone=?,detaildesc=? where id=?";
		List<Object> params = new ArrayList<Object>();
		params.add(mi.getName());
		params.add(mi.getServerLimit());
		params.add(mi.getType());
		params.add(mi.getProvince());
		params.add(mi.getCity());
		params.add(mi.getCounty());
		params.add(mi.getDetailAddr());
		params.add(mi.getPhone());
		params.add(mi.getDetaildesc());
		params.add(mi.getId());
		try {
			db.executeUpdate(sql, params);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			db.closeAll();
		}
	}

	@Override
	public MedicalInstit queryMedicalInstitById(int id) {
		String sql = "select * from medical_instit where id=?";
		List<Object> params = new ArrayList<Object>();
		params.add(id);

		MedicalInstit result = null;
		try {
			ResultSet rs = db.executeQuery(sql, params);
			if (rs != null) {
				while (rs.next()) {
					result = rs2Domain(rs);
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			db.closeAll();
		}
		return result;
	}

	@Override
	public List<MedicalInstit> queryMedical(Map<String, Object> map,
			Integer pageNo, Integer pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append("select * from medical_instit ");

		List<Object> params = new ArrayList<Object>();
		sb = genQuerySql(sb, map, pageNo, pageSize, params);
		
		List<MedicalInstit> result = null;
		try {
			ResultSet rs = db.executeQuery(sb.toString(), params);
			if (rs != null) {
				result = new ArrayList<MedicalInstit>();
				while (rs.next()) {
					MedicalInstit mi = rs2Domain(rs);
					result.add(mi);
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			db.closeAll();
		}
		return result;
	}

	private MedicalInstit rs2Domain(ResultSet rs) throws SQLException {
		MedicalInstit mi = new MedicalInstit();
		mi.setId(rs.getInt("id"));
		mi.setName(rs.getString("name"));
		mi.setServerLimit(rs.getString("serverLimit"));
		mi.setCity(rs.getString("city"));
		mi.setCounty(rs.getString("county"));
		mi.setDetaildesc(rs.getString("detaildesc"));
		mi.setDetailAddr(rs.getString("detailAddr"));
		mi.setPhone(rs.getString("phone"));
		mi.setProvince(rs.getString("province"));
		mi.setType(rs.getInt("type"));
		return mi;

	}

	@Override
	public void deleteMedical(int id) {
		String sql = "delete from medical_instit where id = ?";
		List<Object> params = new ArrayList<Object>();
		params.add(id);
		try{
			db.executeUpdate(sql, params);
		} catch(Exception e ){
			log.error(e.getMessage(),e);
		} finally {
			db.closeAll();
		}
	}

	@Override
	public int queryMedicalCount(Map<String, Object> map) {
		StringBuilder sb = new StringBuilder();
		sb.append("select count(1) as total from medical_instit ");
		List<Object> params = new ArrayList<Object>();
		sb = genQuerySql(sb, map, null, null, params);
		
		int result = 0;
		try {
			ResultSet rs = db.executeQuery(sb.toString(), params);
			if (rs != null) {
				while (rs.next()) {
					result = rs.getInt("total");
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			db.closeAll();
		}
		
		return result;
	}
	
	private StringBuilder genQuerySql(StringBuilder sb,Map<String,Object> map,Integer pageNo,Integer pageSize,List<Object> params){
		if (map != null && !map.isEmpty()) {
			sb.append("where ");
			Set<String> set = map.keySet();
			for (String string : set) {
				sb.append(" ").append(string).append("? ").append("and");
				params.add(map.get(string));
			}
			sb.setLength(sb.length() - 3);
		}
		sb.append(" order by createtime,id desc ");
		if (pageNo != null && pageSize != null) {
			sb.append(" limit ?,?");
			params.add((pageNo.intValue()-1)*pageSize);
			params.add(pageSize.intValue());
		}
		
		return sb;
	}

}
